package com.aegean.socket.apacheMINA.main;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSession;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.aegean.socket.apacheMINA.handler.ServiceMessageHandler;

public class Service {
	private static Logger log = Logger.getLogger(Service.class);
	private static final int port = 8080;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		init_Method();
	}

	private static boolean init_Method() {
		IoAcceptor acceptor = null;
		try {
			acceptor = new NioSocketAcceptor();
			acceptor.getSessionConfig().setReadBufferSize(2048);
			acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
			acceptor.getFilterChain().addFirst("logger", new LoggingFilter());
			acceptor.getFilterChain().addLast(
					"codec",
					new ProtocolCodecFilter(new TextLineCodecFactory(Charset
							.forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
			acceptor.getFilterChain().addLast("stringserialize",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
			acceptor.setHandler(new ServiceMessageHandler());
			acceptor.bind(new InetSocketAddress(port));
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}

}
